package com.zxkj.Exception.handle;
import com.zxkj.Exception.BuildParamException;
import com.zxkj.Exception.HrgException;
import com.zxkj.support.Result;
import com.zxkj.support.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 沈颖峰
 * @Date 2019/6/5 19:42
 * @Description 业务异常拦截器
 */
@RestControllerAdvice
@Slf4j
public class HrgExceptionHandler {

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(HrgException.class)
    public Result handleHrgException(HrgException e) {
        e.printStackTrace();
        Result result = new Result();
        result.setCode(e.getStatus());
        result.setMessage(e.getMessage());
        result.setData(e.getData());
        return result;
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleValidException(MethodArgumentNotValidException e) {
        e.printStackTrace();
        Result result = new Result();
        result.setCode(ResultCode.PARAMS_INVALID);
        result.setMessage(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return result;
    }

    /**
     * 参数异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        Map<String,String> map=new HashMap<>();
        BindingResult bindingResult = e.getBindingResult();
        bindingResult.getFieldErrors().forEach(fieldError -> {
            String message = fieldError.getDefaultMessage();
            String field = fieldError.getField();
            map.put(field,message);
        });
        //e.printStackTrace();
        System.out.println("================================");
        log.error("数据校验出现问题{},异常类型{}",e.getMessage(),e.getClass());
        Result result = new Result();
        result.setCode(ResultCode.PARAMS_INVALID);
        result.setData(map);
        result.setMessage("参数校验异常");
        return result;
    }

    @ResponseBody
    @ExceptionHandler(BuildParamException.class)
    public Result handleBindException(BuildParamException e) {
        return e.getResult();
    }





    /**
     * 未知异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result handleUnknownException(Exception e) {
        e.printStackTrace();
        Result result = new Result();
        result.setCode(ResultCode.INTERNAL_SERVER_ERROR);
        result.setMessage(e.toString());
        return result;
    }

}
